package T;

/**
 * 线程同步和锁的示例
 */
public class synchronizedDemo {
    static int a = 0;

    public static void main(String[] args) {
        extracted();
        extracted();
    }

    //减少重复代码
    private static void extracted() {
        new Thread(() -> {
            while (true) {
                //锁对象(保持最小的颗粒度)
                synchronized (synchronizedDemo.class) {
                    if (a >= 50) break;
                    a++;
                }
                System.out.println(Thread.currentThread().getName() + " is running" + " " + a);
            }
        }).start();
    }
}

